package com.hnevc.mytrafficdemo.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.hnevc.mytrafficdemo.R;
import com.hnevc.mytrafficdemo.entity.Car;
import com.hnevc.mytrafficdemo.util.dialog.RechargeDialog;

import java.util.List;

/**
 * Created by Administrator on 2017/12/20.
 */

public class CarListAdapter extends BaseAdapter{
    private static final String TAG = "CarListAdapter";
    Context mContext;
    List<Car> mCarList;


    public CarListAdapter(Context context, List<Car> carList) {
        mContext = context;
        mCarList = carList;
    }

    @Override
    public int getCount() {
        return mCarList.size();
    }

    @Override
    public Object getItem(int i) {
        return mCarList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return mCarList.get(i).getCarId();
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final Car car = mCarList.get(i);
        view = LayoutInflater.from(mContext).inflate(R.layout.account_list_item,null);
        TextView mTvCarId = (TextView) view.findViewById(R.id.id_tv_id);
        mTvCarId.setText(car.getCarId()+"");
        final CheckBox checkBox = (CheckBox) view.findViewById(R.id.id_cb_recharge);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                car.setNeedCharge(b);
            }
        });
        Button button = (Button) view.findViewById(R.id.id_btn_recharge);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e(TAG, "onClick: "+"recharge");
                clearCheckCars();
                RechargeDialog dialog = new RechargeDialog(mContext,"账户充值",mCarList);
                car.setNeedCharge(true);
                dialog.show();

            }
        });
        return view;
    }

    private void clearCheckCars() {
        for(int i=0;i<mCarList.size();i++){
            mCarList.get(i).setNeedCharge(false);

        }
    }
}
